import { AfterViewInit, Component, Injector, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { Subject } from 'rxjs';
import { listAnimation } from '../../../../animations/list.animation';
import { BaseUI } from '../../../../common/base';
import { TableComponent } from '../../../../components/table/table.component';
import { ColumnType } from '../../../../components/table/enums/ColumnType';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../../../components/table/table-option/table-option.model';
import { DateUtil } from '../../../../utils/DateUtil';
import { GlobalConfig } from '../../../../common/GlobalConfig';
import { JobLogDetailComponent } from '../component/job-log-detail-option/job-log-detail.component';
import { JobService } from '../../job/service/job.service';
import { JobLogService } from '../service/job-log.service';

@Component({
    selector: 'app-job',
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.scss'],
    animations: [listAnimation]
})
export class JobLogComponent extends BaseUI implements OnInit, AfterViewInit, OnDestroy {
    onDestroy$: Subject<any> = new Subject<any>();
    jobId: string = "";
    constructor(
        public _config: ConfigModule,
        private _injector: Injector,
        public mainService: JobLogService,
        private jobService: JobService
    ) {
        super(_injector);
        const urlParts = this.router.url.split('/');
        this.jobId = urlParts[urlParts.length - 1];
        if (this.jobId!=null){
            this.jobService.get(this.jobId).subscribe(res=>{
                this.searchObj.jobName = res.data.jobName;
                this.searchObj.jobGroup = res.data.jobGroup;
                this.searchList();
            })
        }else {
            this.searchList()
        }
    }

    ngOnInit() {
    }

    ngAfterViewInit() {}

    ngOnDestroy() {
        this.onDestroy$.complete();
    }
    /**
     * 表格相关
     */

    @ViewChild("mainTable") mainTable: TableComponent;
    searchObj:any = {
        pageNum: 1,
        deptId: '',
        pageSize: this._config.pageSize,
        name: '',
        phonenumber: '',
        emailNumber: '',
        status: "",
    };

    columnArray = [
        {type: ColumnType.TEXT, name: '日志编号', key: 'jobLogId'},
        {type: ColumnType.TEXT, name: '任务名称', key: 'jobName'},
        {type: ColumnType.TEXT, name: '任务组名', key: 'jobGroup'},
        {type: ColumnType.TEXT, name: '调用目标字符串', key: 'invokeTarget'},
        {type: ColumnType.TEXT, name: '日志信息', key: 'jobMessage'},
        {type: ColumnType.DICT_LABEL, name: '执行状态', key: 'status'},
        {type: ColumnType.TEXT, name: '执行时间', key: 'createTime'},
    ];
    optionModelList: TableOptionModel[] = [
           {
                optionLabel:"详情",
                optionLabelType: OptionTypeEnum.TEXT,
                eventKey:EventKeyEnum.DETAIL
           },
    ];
    otherOptionModelList: TableOptionModel[] = null;

    globalCaseMap = GlobalConfig.globalCaseMap;

    optionEvent($event: EventPublishDTO) {
        let data  = $event.publishData;
        switch ($event.eventKey) {
            case EventKeyEnum.DETAIL:
                    this.jobLogDetailComponent.onModalOpen(data);
                break;

        }
    }

    searchList() {
        this.mainTable.list();
    }

    keyEnter($event: KeyboardEvent) {
        if($event.key === 'Enter'){
            this.searchList();
        }
    }


    /**
     * 日期选择
     */

    datePick: string[]=[];
    onDatePick() {
        this.searchObj.params = {};
        this.searchObj.params['beginTime'] = DateUtil.formatDate(this.datePick[0],"yyyy-MM-dd");
        this.searchObj.params['endTime'] = DateUtil.formatDate(this.datePick[1],"yyyy-MM-dd");
    }

    /**
     * 弹窗相关
     */
    resetSearchObject(){
        this.searchObj = {
            pageNum: 1,
            pageSize: this._config.pageSize,
        };
        this.datePick=[];
    }

    @ViewChild('jobLogDetailComponent') jobLogDetailComponent: JobLogDetailComponent;
}
